package table.col;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import table.Col;
import table.DataTable;
import table.Row;
import table.SetDataParamenters;
import table.inter.IResultCol;
import table.node.BaseNode;
import table.node.DataNode;
import table.node.TableNode;

public class ResultA2BCol extends A2BCol implements IResultCol {

	public void setDataByCols(List<Col> cols) {
	  List<Col> ACols = this.getTableColsByNames(getATable(), getAColNames());
	  List<Col> BCols = this.getTableColsByNames(getBTable(), getBColNames());
	  List<Col> A_BACols = this.getTableColsByNames(getA2BTable(), getA2BAColNames());
	  List<Col> A_BBCols = this.getTableColsByNames(getA2BTable(), getBColNames());
	  this.clearData();
	  if(ACols != null && BCols != null && ACols.size() > 0){
		  
		  if(A_BACols == null || A_BBCols == null){
			  for(int rowIndex=0; rowIndex < ACols.get(0).getDataSize(); rowIndex++){
				  this.getBTable().clearData();
				  this.copyColsFromAtoB(ACols, BCols, rowIndex);
				  this.getBTable().setColsDataByCols(this.getDstColNames(), this.getBColNames());

				  this.addData(this.getColValueFromBTable(this.getBTable()));
			  }
		  } else {
			  for(int rowIndex=0; rowIndex < ACols.get(0).getDataSize(); rowIndex++){
				  
				  this.getA2BTable().clearData();
				  copyColsFromAtoB(ACols,A_BACols,rowIndex);
				  this.getA2BTable().setColsDataByCols(this.getA2BBColNames(), this.getA2BAColNames());
				  
				  this.getBTable().clearData();
				  copyColsFromAtoB(A_BBCols,BCols,-1);
				  this.getBTable().setColsDataByCols(this.getDstColNames(), this.getBColNames());

				  this.addData(this.getColValueFromBTable(this.getBTable()));
			  }
		  }
	  }
	}

	protected DataNode getColValueFromBTable(DataTable bTable){
		List<Col> bCols = this.getTableColsByNames(this.getBTable(), this.getDstColNames());
		DataNode node = new BaseNode();
		node.setNodeValue(bCols.get(0).getDataSize());
		return node;
	}
	
	protected DataNode getColDataFromBTable(DataTable bTable){
		//count the BCols.size
		List<Col> bCols = this.getTableColsByNames(this.getBTable(), this.getDstColNames());
		DataNode node = new TableNode();
		node.setNodeValue(bCols.size());
		
		List<DataNode> subNodes = new ArrayList<DataNode>();
		for(Iterator<Col> colIt = bCols.iterator();colIt.hasNext();){
			Col tCol = colIt.next();
			
			DataNode subNode = new BaseNode();
			subNode.setNodeValue(tCol.getDescripes());
			
			List<DataNode> subSubNodes = new ArrayList<DataNode>();
			subSubNodes.addAll(tCol.getDataList());
			subNode.setSubNodes(subSubNodes);
			
			subNodes.add(subNode);
		}
		node.setSubNodes(subNodes);

		return node;
	}
	
	protected DataNode getColDataFromBTableList(DataTable bTable){
		//count the BCols.size
		List<Col> bCols = this.getTableColsByNames(this.getBTable(), this.getDstColNames());
		DataNode node = new BaseNode();
		node.setNodeValue(bCols.size());
		
		List<DataNode> subNodes = new ArrayList<DataNode>();
		for(int i=0; i < bCols.get(0).getDataSize(); i++){
			DataNode subNode = new BaseNode();
			subNode.setNodeValue("");
			List<DataNode> subSubNodes = new ArrayList<DataNode>();
			for(Iterator<Col> colIt = bCols.iterator();colIt.hasNext();){
				Col tCol = colIt.next();
				subSubNodes.add(tCol.getNode(i));
			}
			subNode.setSubNodes(subSubNodes);
			subNodes.add(subNode);
		}
		node.setSubNodes(subNodes);

		return node;
	}
	  
	protected List<Col> getTableColsByNames(DataTable table, List<String> names){
		  List<Col> cols = new ArrayList<Col>();
		  if(names == null || table == null) return null;
		  for(int i=0; i < names.size(); i++){
			  if(table.getColByName(names.get(i)) != null){
				  cols.add(table.getColByName(names.get(i)));
			  }
		  }
		  return cols;
	  }

	public boolean copyColsFromAtoB(List<Col> aCols, List<Col> bCols, int rowIndex){
		try{
			for(int colIndex=0; colIndex < aCols.size(); colIndex++){
				Col aCol = aCols.get(colIndex);
				Col bCol = bCols.get(colIndex);
				bCol.clearData();
				if(rowIndex < 0 ){
					for(int i=0; i < aCol.getDataSize(); i++){
						Object aColData = aCol.getValue(i);
						bCol.addData(aCol.setNode(aColData));
					}
				} else {
					Object aColData = aCol.getValue(rowIndex);
					bCol.addData(aCol.setNode(aColData));
				}
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	public DataNode run(HttpServletRequest request){
		String action = request.getParameter("colAction");
		DataNode result = null;
		if(action.equals("query")){
			//query relation
			result = this.query(request);
		}
		if(action.equals("add")){
			//add relation
			result.setNodeValue("add");
		}
		if(action.equals("delete")){
			//delete relation
			result.setNodeValue("delete");
			
		}
		if(action.equals("clear")){
			//clear relation
			result.setNodeValue("clear");
		}
		return result;
	}
	
	public DataNode query(HttpServletRequest request){
		
		SetDataParamenters ATParameter = new SetDataParamenters();
		ATParameter.setQueryList(this.getATQueryFromRequest(request));
		if(ATParameter.getQueryList() != null && ATParameter.getQueryList().size() > 0){
			ATParameter.setPageSize(99999);
			this.getATable().setData(ATParameter);
			this.setDataByCols(null);
		} else {
			SetDataParamenters p = new SetDataParamenters(request);
			if(p.getPageSize() <=0 || p.getPageSize() > 10){
				p.setPageSize(10);
			}
			this.getBTable().setData(p);
		}
		return  this.getColDataFromBTable(this.getBTable());
	}
	
	protected  List<String[]> getATQueryFromRequest(HttpServletRequest request){
		List<String[]> s = new ArrayList<String[]>();
		String[] r = null;
		String colName = "";
		String colValue = "";
		try {
			List<Row> ATRowList = Row.getRowsFromResquest(request);
			if(ATRowList != null && ATRowList.size() > 0){
				List<String> AColNames = this.getAColNames();
				if(AColNames!=null && AColNames.size()>0){
					Row dataRow = ATRowList.get(0);
					for(int i=0; i < AColNames.size(); i++){
						colName = AColNames.get(i);
						if(dataRow.hasName(colName)){
							r = new String[3];
							r[0] = colName; 
							r[1] = (String) dataRow.getValueByName(colName);
							r[2] = "=";
							s.add(r);
						} else {
							s.clear();
							return s;
						}
					}
				}
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
		return s;
	}
}
